
package com.rx.mgt.controller;  

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.rx.mgt.domain.*;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SpamService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainInfoExtService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.util.IpUtil;

/** 
 * ClassName:DomainController 
 * Function :
 * Reason   : 
 * Date     :Feb 1, 2017 10:32:51 AM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class DomainController {
    
    @Autowired
    DomainInfoExtService domainInfoExtService;
    @Autowired
    SysLogService sysLogService;
    @Autowired
    DomainService domainService;
    @Autowired
    SpamService spamService;
    
    @RequestMapping(value="/domain-list.htmlx")
    public String  get(ModelMap model,HttpServletRequest request ,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List<DomainInfoExt> list =  domainInfoExtService.getDomainInfoExt(usersBo);
        model.addAttribute("list",list);
        System.out.println("111111111111111");
        return "domain-list";
    }

    /**
     * 域名收发信规则设置
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/domain-rule-add.htmlx")
    public String addRule(HttpServletRequest request,Map map,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List<DomainInfoExt> list =  domainInfoExtService.getDomainInfoExt(usersBo);
        map.put("list",list);
        return "domain-rule-add";
    }
    
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/stopDomain.htmlx")
    @ResponseBody
    public Map  stopDomain(HttpServletRequest request ,DomainInfoExt domainInfoExt){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("停止域", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        DomainInfoExt info= domainInfoExtService.get(domainInfoExt.getId());
        if(info!=null){
            info.setStatus("N");
            domainInfoExtService.update(info);
        }
        System.out.println("111111111111111");
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/startDomain.htmlx")
    @ResponseBody
    public Map  startDomain(HttpServletRequest request ,DomainInfoExt domainInfoExt){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("开启域", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        DomainInfoExt info= domainInfoExtService.get(domainInfoExt.getId());
        if(info!=null){
            info.setStatus("Y");
            domainInfoExtService.update(info);
        }
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/domain-add.htmlx")
    public String  add(ModelMap model,DomainInfoExt domainInfoExt){
        Map map =new HashMap();
        map.put("result", "success");
        if(domainInfoExt.getId()==null||domainInfoExt.getId()<1){
            return "domain-add";
        }
        DomainInfoExt info= domainInfoExtService.get(domainInfoExt.getId());
        if(info!=null){
            model.addAttribute("domainInfoExt",info);
        }
        return "domain-add";
    }

    /**
     *
     * @param request
     * @param
     * @param
     * @return
     */


    @RequestMapping("/domain-detail.htmlx")
    public String domainDetail(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            DomainInfoExt info = domainInfoExtService.getDomainInfoExtByDomain(user.getDomain());
            request.setAttribute("domainInfoExt",info);
            return "domain-detail";
        }else{
            return "error";
        }

    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/domainSave.htmlx")
    public @ResponseBody Map  domainSave(HttpServletRequest request ,ModelMap model,DomainInfoExt domainInfoExt){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("保存域", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        map.put("result", "success");
        DomainInfoExt info=null;
        UsersBo ub = new UsersBo();
        ub.setDomain(domainInfoExt.getDomain());
        List<DomainInfoExt> dieList = domainInfoExtService.getDomainInfoExt(ub);
        if(dieList!=null && dieList.size()>0&&domainInfoExt.getId()==null){
            map.put("code", "2");
            return map;
        }
        if(domainInfoExt.getId()!=null){
            info= domainInfoExtService.get(domainInfoExt.getId());
        }
        if(info!=null){
            domainInfoExtService.update(domainInfoExt);
        }else{
            domainInfoExtService.add(domainInfoExt);
            Spam spam  = new Spam();
            spam.setInsert_date(new Date());
            spam.setStatus("0");
            spam.setBoxId(0);
            spam.setDomain(domainInfoExt.getDomain());
            spamService.saveOrUpdate(spam);
        }
        return map;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteDomain.htmlx")
    @ResponseBody
    public Map  deleteDomain(HttpServletRequest request ,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除域", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                domainInfoExtService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }
    public String  update(HttpServletRequest request ,DomainInfoExt domainInfoExt){
        System.out.println("111111111111111");
        return "index";
    }

    /***
     * 发信频率设置
     */
    @RequestMapping("/send-count-add.htmlx")
    public String sendCountAdd(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        String type = user.getType();
        if("2".equals(type)){
            request.setAttribute("dom",user.getDomain());
        }
        request.setAttribute("domainList",list);
        return "domain_sendcount_add";
    }
    /**
     * ajaxGetDomainExtByDomain.htmlx
     * 异步获取发送频率设置信息
     */
    @RequestMapping("/ajaxGetDomainExtByDomain.htmlx")
    @ResponseBody
    public Map<String,Object> ajaxGetDomainExtByDomain(@RequestParam("domain")String domain,HttpServletRequest request){
        Map<String,Object> rs = new HashMap<String, Object>();
        DomainInfoExt ext = domainInfoExtService.getDomainInfoExtByDomain(domain);
        rs.put("code","001");
        rs.put("obj",ext);
        return rs;

    }
    /**
     * 修改入库 发信间隔时间和限制数量
     */
    @RequestMapping("/updateDomainInfoExtTimeAndCount.htmlx")
    @ResponseBody
    public Map<String,Object> updateDomainInfoExtTimeAndCount(DomainInfoExt domainInfoExt,HttpServletRequest request){
        Map<String,Object> rs = new HashMap<String, Object>();
        String domain = domainInfoExtService.updateDomainInfoExtTimeAndCount(domainInfoExt);
        if(StringUtils.isNotBlank(domain)){
            rs.put("code","001");
        }else{
            rs.put("code","001");
        }
        return rs;
    }

    /**
     * 综合功能开关编辑修改页面
     */
    @RequestMapping("/common-opreation.htmlx")
    public String sysOpreate(HttpServletRequest request,@RequestParam(value ="domain",required = false)String domain){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        if("2".equals(user.getType())){
            domain = user.getDomain();
            request.setAttribute("dom",user.getDomain());
        }
        if(StringUtils.isBlank(domain)){
            if(list!=null&&list.size()>0){
                Domain ext = (Domain)list.get(0);
                domain = ext.getDomain_name();
            }
        }
        SysOpreate opreate = domainService.getSysOpreateByDomain(domain);
        request.setAttribute("sysOpreate",opreate);
        request.setAttribute("domain1",domain);
        request.setAttribute("domainList",list);

        return "common-opreation";
    }

    /**
     * 更新综合功能开关
     */
    @RequestMapping("/updateSysOpreate.htmlx")
    @ResponseBody
    public Map<String,Object> updateSysOpreate(HttpServletRequest request,SysOpreate sysOpreate){
        Map<String,Object> rs = new HashMap<String, Object>();
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        int i =  domainService.updateSysOpreate(sysOpreate);
        if(i>0){
            rs.put("code","001");
        }else{
            rs.put("code","002");
        }
        return rs;
    }
}
  